package com.tuananh.radnom.model.event;

import java.io.ByteArrayOutputStream;
import java.util.Map.Entry;

import javax.xml.transform.stream.StreamResult;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.oxm.Marshaller;
import org.springframework.stereotype.Component;

@Component
public class EventReporter {
	public static Marshaller marshaller;
	public static Marshaller getMarshaller() {
		return marshaller;
	}

	public void setMarshaller(Marshaller marshaller) {
		EventReporter.marshaller = marshaller;
	}

	private static MessageChannel channel;

	@Value("#{eventChannel}")
	public void setChannel(MessageChannel channel) {
		this.channel = channel;
	}

	public static void report(NmsEvent event) {
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		StreamResult streamResult = new StreamResult(os);
		event.marshall(streamResult);
		MessageBuilder<String> message = MessageBuilder
				.withPayload(os.toString()).setHeader("NAME", event.getName());
		System.out.print(event.headers.size());
		for (Entry<String, String> entry : event.headers.entrySet()) {
			message = message.setHeader(entry.getKey(), entry.getValue());
		}
		channel.send(message.build());
	}
}
